require('./PageNotice.styl');
const DB = require('app/db.js');
import classNames from 'classnames';
const reactMixin = require('react-mixin');
const Actions = require('./actions');
const Store = require('./store');
const Search = require('components/search')
const WithRefresh = require('components/withrefresh');
const PendingList = require('components/pendinglist')
const Myslide = require('components/myslide');
const { Toast } = SaltUI;
const ListWithPending = WithRefresh(
    PendingList,
    (pageNum, props, success, error) => {
        let cb = (num) => {
            props.handleBadgeCb && props.handleBadgeCb(num);
        }
        Actions.getData(pageNum, props, success, error, cb)
    }
);
class Notice extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            currentType: 0 //0 搜索项目名称， 1 搜索项目编号
        };
        this.searchRange = ["项目名称", "项目编号"];
        this.handleClickCb = this.handleClickCb.bind(this);
        this.handleClickDelete = this.handleClickDelete.bind(this);
        this.handleChangeSearchType = this.handleChangeSearchType.bind(this);
    }
    onsearch(keyword) {
        this.setState({
            keyword: keyword
        }, () => {
            if (keyword) {
                var props = {
                    pageSize: 10,
                }
                if (!!this.state.currentType) {//全为数字的是项目编号
                    props.proj_num = keyword
                    Actions.getData(0, props)
                } else {
                    props.proj_name = keyword
                    Actions.getData(0, props)
                }
            }
        })
    }
    onexit() {//退出搜索
        this.setState({
            keyword: "",
            listData: []
        })
    }
    handleChangeSearchType(currentType) {
        this.setState({ currentType })
    }
    handleClickCb(notice_id) {
        Actions.updataNotice(notice_id);
    }
    handleClickDelete(notice_id) {
        let t = this;
        let cb = () => {
            Toast.show({
                type: 'success',
                content: '删除成功'
            });
            t.refs.ListWithPending.onRefresh()
        }
        Actions.deleteNotice(notice_id, cb);
    }

    render() {
        let t = this;
        let origin = "workplan/notice"
        return (
            <div className="notice">
                <Search onsearch={this.onsearch.bind(this)} searchRange={this.searchRange} data={this.state.listData}
                    origin={origin} onExit={this.onexit.bind(this)} currentType={t.state.currentType} handleChangeSearchType={this.handleChangeSearchType} />
                {/* {this.state.data.length > 0 && this.state.data.map((item, index) => {
                    return (
                        <Myslide
                            data={item}
                            btnName="删除"
                            className="msg"
                            origin={origin}
                            index={index}
                        />
                    );
                })} */}
                <ListWithPending
                    className="msg"
                    tool={true}
                    origin={origin}
                    leftSlip={true}
                    btnName="删除"
                    cb={this.handleClickCb}
                    delete={this.handleClickDelete}
                    ref="ListWithPending"
                    handleBadgeCb={this.props.handleBadgeCb}
                />


            </div>
        );
    }

    componentWillMount() {
    }

    componentDidMount() {

    }

    componentWillReceiveProps(nextProps) {

    }

    shouldComponentUpdate(nextProps, nextState) {
        return true;
    }

    componentWillUpdate(nextProps, nextState) {
    }

    componentDidUpdate(prevProps, prevState) {

    }

    componentWillUnmount() {
    }
}

reactMixin.onClass(Notice, Reflux.connect(Store));

module.exports = Notice;
